/*
 * Copyright (C) 2010 http://laantiguafragua.blogspot.com
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * SimplePanelUpdate.java
 *
 * Created on 02/24/2010
 */

package simplefileupdate;

import java.util.Hashtable;

/**
 * This class provide a simple graphical component for updates file
 * @author user
 */
public class SimplePanelUpdate extends javax.swing.JPanel implements Runnable {

    /** Creates new form SimplePanelUpdate */
    public SimplePanelUpdate() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jScrollPane1 = new javax.swing.JScrollPane();
        newsTextArea = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();
        reloadActionUpdateButton = new javax.swing.JButton();
        updateButton = new javax.swing.JButton();
        progressBar = new javax.swing.JProgressBar();

        setLayout(new java.awt.BorderLayout());

        newsTextArea.setColumns(20);
        newsTextArea.setEditable(false);
        newsTextArea.setRows(5);
        jScrollPane1.setViewportView(newsTextArea);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel1.setLayout(new java.awt.GridBagLayout());

        reloadActionUpdateButton.setText("Check for updates");
        reloadActionUpdateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reloadActionUpdateButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel1.add(reloadActionUpdateButton, gridBagConstraints);

        updateButton.setText("Update");
        updateButton.setEnabled(false);
        updateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        jPanel1.add(updateButton, gridBagConstraints);

        progressBar.setDoubleBuffered(true);
        progressBar.setStringPainted(true);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel1.add(progressBar, gridBagConstraints);

        add(jPanel1, java.awt.BorderLayout.SOUTH);
    }// </editor-fold>//GEN-END:initComponents

    private void reloadActionUpdateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reloadActionUpdateButtonActionPerformed
        // TODO add your handling code here:
        reloadActionUpdate();
    }//GEN-LAST:event_reloadActionUpdateButtonActionPerformed

    private void updateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateButtonActionPerformed
        // TODO add your handling code here:
        disabledComponents();

        updateAction();
    }//GEN-LAST:event_updateButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea newsTextArea;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JButton reloadActionUpdateButton;
    private javax.swing.JButton updateButton;
    // End of variables declaration//GEN-END:variables
    private SimpleFileUpdate sFileU = new SimpleFileUpdate();
    Hashtable hashT = null;

    private void updateAction() {
        new Thread(sFileU).start();
        new Thread(this).start();
    }

    /**
     *
     */
    public void run() {
        int count = 0;
        Hashtable aHash = sFileU.getActionHash();
        while (sFileU.isRun()) {
            switch (sFileU.getTask()) {
                case SimpleFileUpdate.DELETE:
                    newsTextArea.setText("Delete files...");
                    break;
                case SimpleFileUpdate.RENAME:
                    newsTextArea.setText("Rename files...");
                    break;
                case SimpleFileUpdate.DOWNLOAD:
                    newsTextArea.setText("Download files...");
                    while (sFileU.isRun()) {
                        count = (int) (sFileU.getDownCount() * 100 / sFileU.getDownSize());
                        progressBar.setValue(count);
                    }
                    break;
            }
        }
        progressBar.setValue(100);
        if (sFileU.isReloadRecommended())
        {
            javax.swing.JOptionPane.showMessageDialog(this, "Please reload application");
        }

        enabledComponents();
        reloadActionUpdate();
    }

    /**
     *
     * @param sFileU the sFileU to set
     */
    public void setSimpleFileUpdate(SimpleFileUpdate sFileU) {
        this.sFileU = sFileU;
    }

    /**
     *
     * @param vData
     */
    public void setVerDataSource(VersionData vData) {
        sFileU.setVerDataSource(vData);
    }

    /**
     *
     * @param vData
     */
    public void setVerDataDestiny(VersionData vData) {
        sFileU.setVerDataDestiny(vData);
    }

    /**
     * Method reload the XML file and show the pending task
     */
    public void reloadActionUpdate() {
        disabledComponents();

        try {
            sFileU.calculateActionHash();
            hashT = sFileU.getActionHash();

            newsTextArea.setText(sFileU.toString());
        } catch (Exception e) {
            newsTextArea.setText("Error: " + e);
            e.printStackTrace();
        } finally {
            enabledComponents();
        }
    }

    private void disabledComponents() {
        reloadActionUpdateButton.setEnabled(false);
        updateButton.setEnabled(false);
    }

    private void enabledComponents() {
        reloadActionUpdateButton.setEnabled(true);
        updateButton.setEnabled((hashT != null)
                && (sFileU.isActionPending()));
    }
}
